/*
 * @Author: long-xinyu2 2192763423@qq.com
 * @Date: 2025-03-11 18:47:55
 * @LastEditors: long-xinyu2 2192763423@qq.com
 * @LastEditTime: 2025-04-02 17:55:42
 * @FilePath: \algorithm\algorithm-training\2025_3_11.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
using namespace std;
#define int long long
//using i128 = __int128_t;
inline int inc(int x, int v, int mod) { x += v;return x >= mod ? x - mod : x; }//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const int N = 2e3 + 10, M = 1e5 + 10;
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

int getnum(int x,int y,int n)
{   
    if(n == 0) return 1;
    int tt = (1 << (n - 1));
    int xx = x > tt ? 1 : 0;
    int yy = y > tt ? 1 : 0;
    if(xx == 1 && yy == 1)
    {
        return getnum(x - tt,y - tt,n - 1) + tt * tt;
    }
    else if(xx == 1 && yy == 0)
    {
        return getnum(x - tt,y,n - 1) + tt * tt * 2;
    }
    else if(xx == 0 && yy == 1)
    {
        return getnum(x,y - tt,n - 1) + tt * tt * 3;
    }
    else
    {
        return getnum(x,y,n - 1);
    }
}

array<int,2> getxy(int x,int n)
{
    if(n == 0) return {0,0};
    int tt = (1 << (n - 1));
    array<int,2> res;
    if(x > tt * tt * 3)
    {
        res = getxy(x - tt * tt * 3,n - 1);
        res[1] += tt;
    }
    else if(x > tt * tt * 2)
    {
        res = getxy(x - tt * tt * 2, n - 1);
        res[0] += tt;
    }
    else if(x > tt * tt)
    {
        res = getxy(x - tt * tt,n - 1);
        res[0] += tt;
        res[1] += tt;
    }
    else
    {
        res = getxy(x,n - 1);
    }
    return res;
}

void solve()
{
    int n,q;
    cin >> n >> q;
    while(q--)
    {
        string op;
        cin >> op;
        if(op == "->")
        {
            int x,y;
            cin >> x >> y;
            cout << getnum(x,y,n) << endl;
        }
        else
        {
            int x;
            cin >> x;
            array<int,2> ans = getxy(x,n);
            cout << ans[0] + 1<< ' ' << ans[1] + 1 << endl;
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt;
    cin >> tt;
    //tt = 1;
    while (tt--)
    {
        solve();
    }
}